package org.jeecgframework.tag.core.plugin.tag;

import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.jeecgframework.tag.core.base.TagEnum;
import org.jeecgframework.tag.core.error.ParseRuntimeException;
import org.jeecgframework.tag.core.plugin.vo.PluginTagModel;
import org.jeecgframework.tag.core.util.FreemarkUtil;

/**
 * js 或者css 的tag
 * @author JueYue
 * @date 2014年12月4日 下午11:03:09
 */
public class PluginTag extends PluginTagModel {

    private static final long serialVersionUID = 1L;

    @Override
    public String parseTag() {
        // 插入多语言脚本
        String lang = null;
        if (pageContext != null) {
            HttpSession session = ((HttpServletRequest) this.pageContext.getRequest()).getSession();
            lang = (String) session.getAttribute("lang");
        }
        Map<String, Object> root = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(lang)) {
            root.put("lang", lang);
        }
        String[] strArr = type.split(",");
        for (int i = 0; i < strArr.length; i++) {
            root.put(strArr[i], strArr[i]);
        }
        Writer write = new StringWriter();
        try {
            FreemarkUtil.getTemplate(TagEnum.Plugin).process(root, write);
        } catch (Exception e) {
            throw new ParseRuntimeException("PluginTag 文本解析异常", e.fillInStackTrace());
        }
        return write.toString();
    }
}
